import { defineStore } from 'pinia';
import { usersApi } from '@/api/system/users/users-api';

export const useUserStore = defineStore('userStore', {
    // id: 'userStore', 默认信息
    state: () => ({
        token:'',
        id:'',              // id
        userId:'',          // 用户名
        sex:0,              // 性别
        slogan:'',          // 格言
        nickname:'',        // 昵称
        avatar:'',          // 头像
        roles:'',           // 角色
        phoneNumber:'',     // 电话号码
        eMail:'',           // 邮件
        verification:'',    // 验证码
        state:'',           // 状态
        likesCount:'',      // 点赞数
        commentsCount:'',   // 评论数
        danmuCount:'',      // 弹幕数
        perms:[],           // 权限
    }),
    // Getter 完全等同于 Store 状态的计算值，计算state中的值
    getters:{
    },
    // 设置state中的基本信息
    actions:{ 
        // 设置用户信息
        setUserInfo(data, token) {
            token && (this.token = token);
            this.id = data.id;
            this.userId = data.userId;
            this.sex = data.sex;
            this.slogan = data.slogan;
            this.avatar = data.avatar;
            this.nickname = data.nickname;
            this.roles = data.roles;
            this.phoneNumber = data.phoneNumber;
            this.eMail = data.eMail;
            this.state = data.state;
            this.likesCount = data.likesCount;   
            this.commentsCount= data.commentsCount;      
            this.danmuCount= data.danmuCount; 
            this.perms = data.perms;
        },

        // 获取用户信息
        async getUserInfo() {
            let params = {
                id:this.id,
                type: this.id ? '' : 'token'
            };
            const userInfo = await usersApi.findOne({params});
            this.setUserInfo(userInfo.data);
        },
    },
    persist: [// 在这里进行自定义配置
        {
            paths: ['token'],
            storage: localStorage,
        },
        {
            paths: ['id','eMail','userId','roleId','nickname','avatar', 'slogan'],
            storage: sessionStorage,
        },
    ]
})